﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  UnitManage
 作    者:曹俊、高文华
 编写日期:2009-07-04
 审    核:
 审核日期:
 描    述:部门管理
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Cisoft.IntegrationSystem.BLL;
using Cisoft.IntegrationSystem.Model;

public partial class User_UnitManage : WebUI.Page.BasePage
{
    protected string[] arr = new string[50];
    public static int[] iIsout = new int[1000];   //标志，是否展开


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IsOut();
            ibtnDelete.Attributes.Add("onclick", "return confirm('您确定要删除记录吗？\\n注意：该组下属的子组也将被删除！若组中存在会员将不作删除处理');");
            GetList();
        }
    }


    public void IsOut()
    {
        for (int i = 0; i < 1000; i++)
        {
            iIsout[i] = 0;
        }
    }

    #region 显示列表
    public void GetList()
    {
        DataTable mytable = new DataTable();

        mytable.Columns.Add("GroupID");
        mytable.Columns.Add("Name");
        mytable.Columns.Add("Father");
        mytable.Columns.Add("RecordSort");

        ShowColumns(0, 0, mytable);

        this.gvGroup.DataSource = mytable;
        this.gvGroup.DataBind();
    }

    #endregion

    #region 递归
    /// <summary>
    /// 复选框显示文章栏目（无限级栏目）
    /// </summary>
    /// <param name="iDepth">深度</param>
    ///  <param name="iParentID">父类ID</param>
    private void ShowColumns(int iDepth, int iParentID, DataTable Table)
    {

        Cisoft.IntegrationSystem.Model.UnitMO uMO = new Cisoft.IntegrationSystem.Model.UnitMO();
        Cisoft.IntegrationSystem.BLL.UnitBO uBO = new Cisoft.IntegrationSystem.BLL.UnitBO();

        //定义变量
        int iCount = 0, iSum = 0;;
        DataSet dst = new DataSet();

        ///新逻辑
        dst = new Cisoft.IntegrationSystem.BLL.UserGroupBO().GetListNewByRecordSort(" UG_GroupParentID = " + iParentID + "");

        iCount = dst.Tables[0].Rows.Count;

        if (iCount != 0)
        {
            //iSum深度变量
            iSum = iDepth + 1;

            for (int i = 0; i < iCount; i++)
            {
                DataRow newRow = Table.NewRow();

                DataSet dst_b = new DataSet();

                //dst_b = uBO.GetList("Father = " + int.Parse(dst.Tables[0].Rows[i]["GroupID"].ToString()) + "");
                ///新逻辑
                dst_b = new Cisoft.IntegrationSystem.BLL.UserGroupBO().GetListNewByRecordSort("UG_GroupParentID = " + int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()) + "");

                int Count = dst_b.Tables[0].Rows.Count;

                if (Count > 0)
                {
                    newRow["GroupID"] = dst.Tables[0].Rows[i]["UG_GroupID"].ToString();
                    arr[iSum] = "&nbsp;&nbsp;";

                    newRow["Name"] = GetDepth(arr, iSum) + "<img src='../images/user/step1.gif' border='0'>├" + dst.Tables[0].Rows[i]["UG_GroupName"].ToString();


                    newRow["Father"] = int.Parse(dst.Tables[0].Rows[i]["UG_GroupParentID"].ToString());
                    newRow["RecordSort"] = int.Parse(dst.Tables[0].Rows[i]["UG_GroupOrder"].ToString());

                }
                else
                {
                    newRow["GroupID"] = dst.Tables[0].Rows[i]["UG_GroupID"].ToString();
                    arr[iSum] = "│";

                    newRow["Name"] = GetDepth(arr, iSum) + "<img src='../images/user/step2.gif' border='0'>├" + dst.Tables[0].Rows[i]["UG_GroupName"].ToString();

                    newRow["Father"] = int.Parse(dst.Tables[0].Rows[i]["UG_GroupParentID"].ToString());
                    newRow["RecordSort"] = int.Parse(dst.Tables[0].Rows[i]["UG_GroupOrder"].ToString());

                }
                if (iIsout[int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString())] != 0)
                {
                    newRow["Name"] = GetDepth(arr, iSum) + "<img src='../images/user/step2.gif' border='0'>├" + dst.Tables[0].Rows[i]["UG_GroupName"].ToString();


                    newRow["Father"] = int.Parse(dst.Tables[0].Rows[i]["UG_GroupParentID"].ToString());
                    newRow["RecordSort"] = int.Parse(dst.Tables[0].Rows[i]["UG_GroupOrder"].ToString());
                }

                Table.Rows.Add(newRow);

                if (iIsout[int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString())] != 0)
                {
                    ShowColumns(iSum, Int32.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()), Table);
                }
                //ShowColumns(iSum, Int32.Parse(myDataSet.Tables[0].Rows[i]["ColumnID"].ToString()), Table);
            }
        }
    }
    /// <summary>
    /// 字符串插入"│"字符
    /// </summary>
    /// <param name="str">需要插入"│"的字符串</param>
    /// <param name="i">插入"│"字符串的索引位置</param>
    /// <returns></returns>
    private static string GetDepth(string[] arrA, int iCount)
    {
        string strName = "";

        for (int i = 1; i < iCount; i++)
        {
            strName = strName.Insert(strName.Length, arrA[i].ToString() + "&nbsp;");
        }

        return strName;
    }

    #endregion

    #region gridview行命令
    protected void gvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Group_expended")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = this.gvGroup.Rows[index];

            string flag = ((Label)row.FindControl("label1")).Text;
            if (iIsout[int.Parse(flag)] == 0)
            {
                iIsout[int.Parse(flag)] = 1;
            }
            else
            {
                iIsout[int.Parse(flag)] = 0;
            }
            GetList();
        }

    }
    protected void gvGroup_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton Lbtn_a = (LinkButton)e.Row.FindControl("lbtnGroupExpended");
            Lbtn_a.CommandArgument = e.Row.RowIndex.ToString();
        }
    }
    #endregion

    #region 删除,递归删除
    //获取ID号集的方法：
    //遍历所有DataGrid中的所有行（Items），
    //取得指定单元格（Cells），然后找到该单元格的控件（Controls）
    public ArrayList getIDs()
    {
        ArrayList myArrayList = new ArrayList();
        CheckBox box;
        for (int i = 0; i < this.gvGroup.Rows.Count; i++)
        {
            box = gvGroup.Rows[i].FindControl("CheckBoxItem") as CheckBox;

            if (box.Checked)
            {
                myArrayList.Add(Convert.ToInt32(((Label)this.gvGroup.Rows[i].FindControl("label1")).Text));
            }
        }
        return myArrayList;
    }
    /// <summary>
    /// 根据ID号成批删除
    /// </summary>
    /// <param name="al">ID号列表</param>
    public virtual void DeleteBated(ArrayList myArrayList)
    {
        int id;

        for (int i = 0; i < myArrayList.Count; i++)
        {
            id = (int)myArrayList[i];     //获取的主题ID
            this.DeleteOne(id);  //删除本主题
        }
    }
    /// <summary>
    /// 根据ID删除记录,如果ID为-1，则删除全部
    /// </summary>
    /// <param name="id">ID号</param>
    public void DeleteOne(int id)
    {
        DeleteItem(id);
    }

    //删除一条记录
    public void DeleteItem(int id)
    {
        DeleteLevel(id);
    }
    //删除所有下级

    #region 删除所有下级

    public void DeleteLevel(int id)
    {
        string strError = "";
        UserGroupBO bllMemberGroupfordelete = new UserGroupBO();
        UserGroupMO bllMemberfordeletegroup = new UserGroupMO();

        DataSet dst = new DataSet();
        //dst = bllMemberGroupfordelete.GetParent(id);

        try
        {
            bllMemberGroupfordelete.Delete(id);
            GetList();
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
        if (strError != "")
        {
            PageError(strError, Request.RawUrl);
        }
        else
        {
            PageRight("删除成功！", "UnitManage.aspx", "", "返回管理");
        }

        //if (dst.Tables[0].Rows.Count > 0)
        //{
        //    for (int i = 0; i < dst.Tables[0].Rows.Count; i++)
        //    {
        //        DataSet dst_b = new DataSet();
        //        dst_b = bllMemberGroupfordelete.GetParent(int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()));
        //        if (dst_b.Tables[0].Rows.Count > 0)
        //        {
        //            DeleteLevel(int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()));
        //        }
        //        else
        //        {
        //            #region 删除该部门下所有员工
        //            //DataSet dsformemberdelete = bllMemberfordeletegroup.GetMemberList("MB_GroupID_Rid=" + int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()));

        //            //if (dsformemberdelete.Tables[0].Rows.Count > 0)
        //            //{

        //            //}
        //            //else
        //            //{
        //            //    bllMemberGroupfordelete.Delete(int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()));
        //            //}
        //            #endregion
        //        }
        //    }
        //}

        //DataSet dsformemberdelete2 = bllMemberfordeletegroup.GetMemberList("MB_GroupID_Rid=" + id);

        //if (dsformemberdelete2.Tables[0].Rows.Count > 0)
        //{

        //}
        //else
        //{
        //    bllMemberGroupfordelete.Delete(id);
        //}
    }

    #endregion

    #endregion

    #region 删除按钮事件
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList myArrayList = getIDs();

        if (myArrayList.Count != 0)
        {
            DeleteBated(myArrayList);
            GetList();
        }
        else
        {
            //string str = "请选择要删除的记录！";
            //PageError(str, "MemberGroupManage.aspx", "返回操作", "");
            PageError("请选择要删除的记录", Request.RawUrl);
        }

    }
    #endregion

    #region 新增加按钮事件
    protected void Button_Add_Click(object sender, EventArgs e)
    {
        Response.Redirect("UnitAdd.aspx");
    }
    #endregion

    #region 鼠标移到GridView颜色
    protected void gvGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        CheckBox1.Checked = false;

        for (int i = -1; i < gvGroup.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
    #endregion

    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("UnitAdd.aspx");
    }
}
